#!/data/Software/mydan/perl/bin/perl -I/data/Software/mydan/JOB/lib -I/data/Software/mydan/JOB/private/lib
use strict;
use warnings;
use Code;
use Logs;

$| ++;

=head1 SYNOPSIS

    id => 1,
    db => $db,
    node => node1,node2  or node => [ 'node1', 'node2' ]
    logs => 日志对象

    #用于检查项目编号下的任务允许使用的机器范围，id 为项目编号,node为要检查的机器，如果返回undef表示验证通过
    
=cut

my $code;
BEGIN { $code = Code->new( 'nodeinfo' ); };

return sub
{
    my %param = @_;
    my ( $db, $id, $node, $logs )= @param{qw( db id node logs )};

    $logs = Logs->new( 'code.nodeinfo_check' ) unless $logs;
    $logs->die( "id not a number" ) unless $id =~ /^\d+$/;

    $logs->die( "node undef" ) unless defined $node;

    $node = [ split /,/, $node ] unless ref $node;

    return undef unless @$node;

    my %node;
    map{ 
        $node{$_->{name}} = 1;
        map{$node{$_}=1;}split /,/, $_->{ip} if defined $_->{ip};
        map{$node{$_}=1;}split /,/, $_->{exip} if defined $_->{exip};
        map{$node{$_}=1;}split /,/, $_->{inip} if defined $_->{inip};
    } $code->run( id => $id, db => $db );

    my @fail;
    map{ push( @fail, $_ ) unless $node{$_} }@$node;
    return @fail ? join( ',', @fail ) : undef;
}
